home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / share / gufw / model / FrontEnd.py < prev    next >
Encoding:
Python Source  |  2009-10-25  |  10.5 KB  |  304 lines

  1. # Gufw 9.10.4 - http://gufw.tuxfamily.org
  2. # Copyright (C) 2009 Marcos Alvarez Costales
  3. #
  4. # Gufw is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 3 of the License, or
  7. # (at your option) any later version.
  8. # Gufw is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11. # GNU General Public License for more details.
  12. # You should have received a copy of the GNU General Public License
  13. # along with Gufw; if not, see http://www.gnu.org/licenses for more
  14. # information.
  15.  
  16.  
  17. import commands
  18. from Variable import Variable
  19.  
  20.  
  21. class Frontend:
  22.  
  23.     def __init__(self):
  24.         self.variable = Variable()
  25.  
  26.        
  27.     # Return status ufw
  28.     def set_status(self, status):
  29.         commands.getstatusoutput(self.variable.get_command(status))
  30.         return status
  31.  
  32.  
  33.     # Get status ufw
  34.     def get_status(self):
  35.         status = commands.getstatusoutput(self.variable.get_command("status"))
  36.         if status[1].find("Status: active") != -1:
  37.             return self.variable.get_constant("enabled")
  38.         else:
  39.             return self.variable.get_constant("disabled")
  40.  
  41.  
  42.     # Return status ufw
  43.     def set_default(self, default):
  44.         commands.getstatusoutput(self.variable.get_command(default))
  45.         return default
  46.  
  47.  
  48.     # Get status ufw
  49.     def get_default(self):
  50.         default = commands.getstatusoutput(self.variable.get_command("search_policy"))
  51.         if default[1].find("ACCEPT") != -1:
  52.             return self.variable.get_constant("allow")
  53.         elif default[1].find("DROP") != -1:
  54.             return self.variable.get_constant("deny")
  55.         elif default[1].find("REJECT") != -1:
  56.             return self.variable.get_constant("reject")
  57.  
  58.  
  59.     # Get actual rules (NOT SPLIT BY ACTION)
  60.     def get_rules(self):
  61.         rules        = commands.getstatusoutput(self.variable.get_command("status"))
  62.         rule_lines   = rules[1].split("\n")
  63.         return_rules = []
  64.         
  65.         for descomponent_rules in rule_lines:
  66.             
  67.             if descomponent_rules.find(self.variable.get_constant("allow_upper"))  != -1 or \
  68.                descomponent_rules.find(self.variable.get_constant("deny_upper"))   != -1 or \
  69.                descomponent_rules.find(self.variable.get_constant("limit_upper"))  != -1 or \
  70.                descomponent_rules.find(self.variable.get_constant("reject_upper")) != -1:
  71.                 
  72.                 return_rules.append(descomponent_rules)
  73.                 
  74.         return return_rules
  75.  
  76.  
  77.     # Return status ufw
  78.     def set_ufw_log(self, status_ufw_log):
  79.         commands.getstatusoutput(self.variable.get_command(status_ufw_log))
  80.         return status_ufw_log
  81.  
  82.  
  83.     # Get status ufw
  84.     def get_ufw_log(self):
  85.         status = commands.getstatusoutput(self.variable.get_command("status_verbose"))
  86.         if status[1].find("Logging: on (high)") != -1:
  87.             return self.variable.get_constant("ufw_log_high")
  88.         elif status[1].find("Logging: on (medium)") != -1:
  89.             return self.variable.get_constant("ufw_log_medium")
  90.         elif status[1].find("Logging: on (low)") != -1:
  91.             return self.variable.get_constant("ufw_log_low")
  92.         elif status[1].find("Logging: on (full)") != -1:
  93.             return self.variable.get_constant("ufw_log_full")
  94.         else:
  95.             return self.variable.get_constant("ufw_log_off")
  96.  
  97.  
  98.     # Return status ufw
  99.     def set_gufw_log(self, status_gufw_log):
  100.         if not self.variable.dev:
  101.             status_gufw_log_aux = status_gufw_log
  102.         else:
  103.             status_gufw_log_aux = status_gufw_log + "_dev"
  104.             
  105.         commands.getstatusoutput(self.variable.get_command(status_gufw_log_aux))
  106.         return status_gufw_log
  107.  
  108.  
  109.     # Get status ufw
  110.     def get_gufw_log(self):
  111.         if not self.variable.dev:
  112.             command = commands.getstatusoutput(self.variable.get_command("cfg_gufw_log"))
  113.         else:
  114.             command = commands.getstatusoutput(self.variable.get_command("cfg_gufw_log_dev"))
  115.             
  116.         if command[0] == 0:
  117.             return self.variable.get_constant("gufw_log_on")
  118.         else:
  119.             return self.variable.get_constant("gufw_log_off")
  120.  
  121.  
  122.     # Add rule 
  123.     def add_rule_component(self, service, action, protocol, fromip, fromport, toip, toport):
  124.         # Component rule
  125.         if service == self.variable.get_constant("service_no"):
  126.             rule = "LANG=en ufw &action proto &protocol from &fromIP port &fromPort to &toIP port &toPort"
  127.         else:
  128.             rule = "LANG=en ufw &action &toPort"
  129.         
  130.         # Action
  131.         rule = rule.replace("&action", action)
  132.         
  133.         # Protocol
  134.         if protocol != self.variable.get_constant("both"):
  135.             rule = rule.replace("&protocol", protocol)
  136.         else:
  137.             rule = rule.replace(" proto &protocol ", " ")
  138.             
  139.         # FROM
  140.         if fromip != "":
  141.             rule = rule.replace("&fromIP", fromip)
  142.         else:
  143.             rule = rule.replace("&fromIP", self.variable.get_constant("any"))
  144.             
  145.         if fromport != "":
  146.             rule = rule.replace("&fromPort", fromport)
  147.         else:
  148.             rule = rule.replace(" port &fromPort ", " ")
  149.             
  150.         # TO
  151.         if toip != "":
  152.             rule = rule.replace("&toIP", toip)
  153.         else:
  154.             rule = rule.replace("&toIP", self.variable.get_constant("any"))
  155.             
  156.         if toport != "":
  157.             rule = rule.replace("&toPort", toport)
  158.         else:
  159.             rule = rule.replace(" port &toPort", "")
  160.  
  161.         # Return rule command
  162.         return rule
  163.  
  164.  
  165.     # Command Add rule
  166.     def add_rule(self, rule):
  167.         result = commands.getstatusoutput(rule)
  168.         return result[1]
  169.  
  170.  
  171.     # Add rule 
  172.     def remove_rule_component(self, rule_to, action, rule_from):
  173.         # Component rule
  174.         protocol = ""
  175.         toip     = ""
  176.         toport   = ""
  177.         fromip   = "" 
  178.         fromport = ""
  179.         rule   = "LANG=en ufw delete &action proto &protocol from &fromIP port &fromPort to &toIP port &toPort"
  180.         
  181.         # Set protocol
  182.         if rule_to.find("/tcp")   != -1 or rule_from.find("/tcp") != -1:
  183.             protocol = self.variable.get_constant("tcp")
  184.             rule_to   = rule_to.replace("/tcp","")
  185.             rule_from = rule_from.replace("/tcp","")
  186.         elif rule_to.find("/udp")   != -1 or rule_from.find("/udp") != -1:
  187.             protocol = self.variable.get_constant("udp")
  188.             rule_to   = rule_to.replace("/udp","")
  189.             rule_from = rule_from.replace("/udp","")
  190.         else:
  191.             protocol = self.variable.get_constant("both")
  192.  
  193.         
  194.         # Set IP/Port TO
  195.         to_aux = rule_to.split(" ")
  196.         if len(to_aux) == 1:
  197.             # IP or Port?
  198.             if to_aux[0] == self.variable.get_constant("anywhere"):
  199.                 toip = ""
  200.             elif ( to_aux[0].find(".") != -1 ) or ( len(to_aux[0]) > 11 ):
  201.                 toip = to_aux[0]
  202.             else:
  203.                 toport = to_aux[0]
  204.         elif len(to_aux) == 2:
  205.             toip   = to_aux[0]
  206.             toport = to_aux[1]
  207.         elif len(to_aux) == 3:
  208.             # It's a app command
  209.             rule = "LANG=en ufw delete " + to_aux[2]
  210.             return rule
  211.             
  212.         # Set IP/Port FROM
  213.         from_aux = rule_from.split(" ")
  214.         if len(from_aux) == 1:
  215.             # IP or Port?
  216.             if from_aux[0] == self.variable.get_constant("anywhere"):
  217.                 fromip = ""
  218.             elif ( from_aux[0].find(".") != -1 ) or ( len(from_aux[0]) > 11 ):
  219.                 fromip = from_aux[0]
  220.             else:
  221.                 fromport = from_aux[0]
  222.         elif len(from_aux) == 2:
  223.             fromip   = from_aux[0]
  224.             fromport = from_aux[1]
  225.         elif len(from_aux) == 3:
  226.             # It's a app command
  227.             rule = "LANG=en ufw delete " + from_aux[2]
  228.             return rule
  229.             
  230.         # Set rule    
  231.         # Action
  232.         rule = rule.replace("&action", action)
  233.         
  234.         # Protocol
  235.         if protocol != self.variable.get_constant("both"):
  236.             rule = rule.replace("&protocol", protocol)
  237.         else:
  238.             rule = rule.replace(" proto &protocol ", " ")
  239.             
  240.         # FROM
  241.         if fromip != "":
  242.             rule = rule.replace("&fromIP", fromip)
  243.         else:
  244.             rule = rule.replace("&fromIP", self.variable.get_constant("any"))
  245.             
  246.         if fromport != "":
  247.             rule = rule.replace("&fromPort", fromport)
  248.         else:
  249.             rule = rule.replace(" port &fromPort ", " ")
  250.             
  251.         # TO
  252.         if toip != "":
  253.             rule = rule.replace("&toIP", toip)
  254.         else:
  255.             rule = rule.replace("&toIP", self.variable.get_constant("any"))
  256.             
  257.         if toport != "":
  258.             rule = rule.replace("&toPort", toport)
  259.         else:
  260.             rule = rule.replace(" port &toPort", "")
  261.  
  262.         # Return rule command
  263.         return rule
  264.  
  265.  
  266.     # Command Add rule
  267.     def remove_rule(self, rule):
  268.         result = commands.getstatusoutput(rule)
  269.         return result[1]
  270.  
  271.  
  272.     # Return size Gufw window
  273.     def get_old_size_window(self):
  274.         if not self.variable.dev:
  275.             command = self.variable.get_command("read_size_win")
  276.         else:
  277.             command = self.variable.get_command("read_size_win_dev")
  278.             
  279.         width_height = commands.getstatusoutput(command)
  280.         if width_height[0] == 0:
  281.             width_height_split = width_height[1].split(";")
  282.             width  = width_height_split[0].replace("width=","")
  283.             height = width_height_split[1].replace("height=","")
  284.         else:
  285.             width  = self.variable.get_constant("window_width")
  286.             height = self.variable.get_constant("window_height")
  287.         
  288.         return int(width),int(height)
  289.         
  290.  
  291.     # Save actual size window
  292.     def save_size_window(self, win_width, win_height):
  293.         if not self.variable.dev:
  294.             command = self.variable.get_command("save_size_win")
  295.         else:
  296.             command = self.variable.get_command("save_size_win_dev")
  297.         
  298.         command = command.replace("&1", str(win_width))
  299.         command = command.replace("&2", str(win_height))
  300.         
  301.         commands.getstatusoutput(command)
  302.